    # -*- coding = utf-8 -*-
#@Time: 2020/10/23 11:36
#@Author: 卜白
#@File: view.py
#@Software: PyCharm

from sqlalchemy import or_
from apps.house.models import *
from apps.user.models import *
from flask import Blueprint, render_template, request, redirect, url_for, jsonify,session,g

house_bp = Blueprint('house_bp',__name__,url_prefix='/house')

# 城市
@house_bp.route('/',endpoint='city')
def city():
    return render_template('house/city.html')

# 验证登录用户
def verification():
    # 登录用户
    user = None
    user_id = session.get('userid', None)
    if user_id:
        user = User.query.get(user_id)
        print('用户：', user)

    return user

# 首页
@house_bp.route('/index', methods=['GET','POST'])
def index():
    # 验证登录用户
    user = verification()

    if request.method == 'GET':
        city = request.args.get('city')    # 接收city页面传来的搜索城市
        session['city'] = city  # 将city加入会话
        # 租房
        rent_data = rent_house.query.filter(rent_house.city_name == city).all()
        rent_num = len(rent_data)

        # 新房
        new_data = new_house.query.filter(new_house.city_name == city).all()
        new_num = len(new_data)

        # 在售二手房
        sell_data = sell_eshouse.query.filter(sell_eshouse.city_name == city).all()
        sell_num = len(sell_data)

        # 成交二手房
        deal_data = deal_eshouse.query.filter(deal_eshouse.city_name == city).all()
        deal_num = len(deal_data)
        return render_template('house/index.html',user=user,
            city=city,rent_data=rent_data,rent_num=rent_num,
            new_data=new_data,new_num=new_num,sell_data=sell_data,sell_num=sell_num,
            deal_data=deal_data,deal_num=deal_num
        )

# 在售-二手房
@house_bp.route('/ershoufang/<city>')
def ershoufang(city):   # city城市名
    # 验证登录用户
    user = verification()

    # 在售二手房-详情页
    sell_id = request.args.get('sellid')
    if sell_id:
        sell_details = sell_eshouse.query.filter(sell_eshouse.sell_id == sell_id)
        datas = sell_details.first()
        return render_template('house/ershoufang/selldetail.html',user=user,datas=datas,city=city)

    # 在售二手房-列表页分页
    page = int(request.args.get('page', 1))
    sellesf = sell_eshouse.query.filter(sell_eshouse.city_name == city)        # city的在售二手房查询语句
    areas = sellesf.group_by(sell_eshouse.area_name).all()    # city分组查询area区域名
    if areas:
        datas = sellesf.all()  # city查询的列表数据
        sell_num = len(datas) # 总数
        # 翻页
        data_list = sellesf.paginate(page=page, per_page=30)
        return render_template('house/ershoufang/sellhouse.html',user=user,
                               city=city,datas=datas,areas=areas,sell_num=sell_num,data_list=data_list)

# 成交-二手房
@house_bp.route('/chengjiao/<city>')
def dealershouhouse(city):
    # 验证登录用户
    user = verification()

    # 成交二手房-详情页
    deal_id = request.args.get('dealid')
    if deal_id:
        deal_details = deal_eshouse.query.filter(deal_eshouse.deal_id == deal_id)
        datas = deal_details.first()
        return render_template('house/ershoufang/dealdetail.html',user=user,datas=datas,city=city)

    page = int(request.args.get('page', 1))
    # .order_by(-deal_eshouse.deal_time)按最新成交时间降序
    dealesf = deal_eshouse.query.filter(deal_eshouse.city_name == city).order_by(-deal_eshouse.deal_time)
    areas = dealesf.group_by(deal_eshouse.area_name).all()
    if areas:
        datas = dealesf.all()   # 成交二手房总列表数据
        deal_num = len(datas)   # 总数
        # 翻页
        data_list = dealesf.paginate(page=page, per_page=30)
        return render_template('house/ershoufang/dealhouse.html',user=user,
                               city=city,datas=datas,areas=areas,deal_num=deal_num,data_list=data_list)

# 新房
@house_bp.route('/loupan/<city>')
def newhouse(city):
    # 验证登录用户
    user = verification()

    # 新房列表页
    page = int(request.args.get('page', 1))
    new_lp = new_house.query.filter(new_house.city_name == city)
    areas = new_lp.group_by(new_house.area_name).all()  # 区域
    if areas:
        datas = new_lp.all()    # 新房列表页总数据
        new_num = len(datas)    # 总数
        # 翻页
        data_list = new_lp.paginate(page=page, per_page=18)

        # 新房详情页
        new_id = request.args.get('newid')
        if new_id:
            new_details = new_house.query.filter(new_house.house_unique == new_id)
            datas = new_details.first()
            return render_template('house/newhouse/newdetail.html',user=user, datas=datas, city=city)

        return render_template('house/newhouse/newhouse.html',user=user,
                               city=city,areas=areas,datas=datas,new_num=new_num,data_list=data_list)

# 租房
@house_bp.route('/renthouse/<city>')
def renthouse(city):
    # 验证登录用户
    user = verification()

    # 租房列表页
    page = int(request.args.get('page', 1))
    rents = rent_house.query.filter(rent_house.city_name == city)
    areas = rents.group_by(rent_house.area_name).all()  # 区域
    if areas:
        datas = rents.all()    # 租房列表页总数据
        rent_num = len(datas)    # 总数
        # 翻页
        data_list = rents.paginate(page=page, per_page=30)

        # 租房详情页
        rent_id = request.args.get('rentid')
        if rent_id:
            rent_details = rent_house.query.filter(rent_house.rent_id == rent_id)
            datas = rent_details.first()
            return render_template('house/renthouse/rentdetail.html',user=user, datas=datas, city=city)

        return render_template('house/renthouse/renthouse.html',user=user,
                               city=city,areas=areas,datas=datas,rent_num=rent_num,data_list=data_list)

